﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace btnet.pages
{
    public partial class massedit : CommonPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Bug_Util.do_not_cache(Response);

            security = new Security();

            security.check_security(HttpContext.Current, Security.ANY_USER_OK_EXCEPT_GUEST);

            if (security.user.is_admin || security.user.can_mass_edit_bugs)
            {
                //
            }
            else
            {
                Response.Write("You are not allowed to use this page.");
                Response.End();
            }


            string list = "";

            if (!IsPostBack)
            {
                titl.Text = Bug_Util.get_setting("AppTitle", "BugTracker.NET") + " - "
                    + "massedit";

                if (Request["mass_delete"] != null)
                {
                    update_or_delete.Value = "delete";
                }
                else
                {
                    update_or_delete.Value = "update";
                }

                // create list of bugs affected
                foreach (string var in Request.QueryString)
                {
                    if (Bug_Util.is_int(var))
                    {
                        if (list != "")
                        {
                            list += ",";
                        }
                        list += var;
                    };
                }

                bug_list.Value = list;

                if (update_or_delete.Value == "delete")
                {
                    update_or_delete.Value = "delete";

                    sql += "delete bug_post_attachments from bug_post_attachments inner join bug_posts on bug_post_attachments.bpa_post = bug_posts.bp_id where bug_posts.bp_bug in (" + list + ")";
                    sql += "\ndelete from bug_posts where bp_bug in (" + list + ")";
                    sql += "\ndelete from bug_subscriptions where bs_bug in (" + list + ")";
                    sql += "\ndelete from bug_relationships where re_bug1 in (" + list + ")";
                    sql += "\ndelete from bug_relationships where re_bug2 in (" + list + ")";
                    sql += "\ndelete from bug_user_flags where fl_bug in (" + list + ")";
                    sql += "\ndelete from bug_user_seen where sn_bug in (" + list + ")";
                    sql += "\ndelete from bug_tasks where tsk_bug in (" + list + ")";
                    sql += "\ndelete from bugs where bg_id in (" + list + ")";

                    confirm_href.InnerText = "Confirm Delete";

                }
                else
                {
                    update_or_delete.Value = "update";

                    sql = "update bugs \nset ";

                    string updates = "";

                    string project = Request["mass_project"];
                    if (project != "-1" && Bug_Util.is_int(project))
                    {
                        if (updates != "") { updates += ",\n"; }
                        updates += "bg_project = " + project;
                    }
                    string org = Request["mass_org"];
                    if (org != "-1" && Bug_Util.is_int(org))
                    {
                        if (updates != "") { updates += ",\n"; }
                        updates += "bg_org = " + org;
                    }

                    string category = Request["mass_category"];
                    if (category != "-1" && Bug_Util.is_int(category))
                    {
                        if (updates != "") { updates += ",\n"; }
                        updates += "bg_category = " + category;
                    }

                    string priority = Request["mass_priority"];
                    if (priority != "-1" && Bug_Util.is_int(priority))
                    {
                        if (updates != "") { updates += ",\n"; }
                        updates += "bg_priority = " + priority;
                    }

                    string assigned_to = Request["mass_assigned_to"];
                    if (assigned_to != "-1" && Bug_Util.is_int(assigned_to))
                    {
                        if (updates != "") { updates += ",\n"; }
                        updates += "bg_assigned_to_user = " + assigned_to;
                    }

                    string status = Request["mass_status"];
                    if (status != "-1" && Bug_Util.is_int(status))
                    {
                        if (updates != "") { updates += ",\n"; }
                        updates += "bg_status = " + status;
                    }


                    sql += updates + "\nwhere bg_id in (" + list + ")";

                    confirm_href.InnerText = "Confirm Update";

                }

                sql_text.InnerText = sql;

            }
            else // postback
            {
                list = bug_list.Value;

                if (update_or_delete.Value == "delete")
                {
                    string upload_folder = Bug_Util.get_upload_folder();
                    if (upload_folder != null)
                    {
                        string sql2 = @"select bp_bug, bp_id, bp_file from bug_posts where bp_type = 'file' and bp_bug in (" + bug_list.Value + ")";
                        DataSet ds = DbUtil.get_dataset(sql2);
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            // create path
                            StringBuilder path = new StringBuilder(upload_folder);
                            path.Append("\\");
                            path.Append(Convert.ToString(dr["bp_bug"]));
                            path.Append("_");
                            path.Append(Convert.ToString(dr["bp_id"]));
                            path.Append("_");
                            path.Append(Convert.ToString(dr["bp_file"]));
                            if (System.IO.File.Exists(path.ToString()))
                            {
                                System.IO.File.Delete(path.ToString());
                            }
                        }
                    }
                }


                DbUtil.execute_nonquery(sql_text.InnerText);
                Response.Redirect("search.aspx");

            }
        }
    }
}
